/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 8:46:30
 * kws
 */

package com.crayon.account.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.crayon.util.GDB;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

public class DataBaseManager extends OrmLiteSqliteOpenHelper {

	private static DataBaseManager helper = null;
	private static final AtomicInteger usageCounter = new AtomicInteger(0);
	private static final String DATABASE_NAME = "crayonaccount.sqlite";
	private static final int DATABASE_VERSION = 2;//

	private Dao<DAOAccount, Integer> daoAccount = null;
	private Dao<DAOCategory,Integer> daoCategory=null;
	private Dao<DAOCardBank,Integer> daoCardBank=null;
	private Dao<DAOPhotos,Integer> daoPhoto=null;
    private Dao<DAOFood,Integer> daoFood=null;
    private Dao<DAOTable,Integer> daoTable=null;

	public DataBaseManager(Context context,String dbName) {
		super(context,dbName, null, DATABASE_VERSION);
	}
	private Context context;

	public static synchronized DataBaseManager getHelper(Context context) {
		if (helper == null) {
			helper = new DataBaseManager(context,DATABASE_NAME);
		}
		usageCounter.incrementAndGet();
		return helper;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
		// TODO Auto-generated method stub
        createTable();
	}
	public void initData(Context context){


	}
//    ArrayList<Class> tables = Arrays.asList(DAOAccount.class,DAOPhotos.class,DAOCategory.class,
//            DAOCardBank.class,DAOFood.class,DAOTable.class);

    private void createTable(){

//        for(Class cls : tables) {
//            try {
//            TableUtils.createTable(connectionSource, cls);
//            } catch (Exception e) {
//                // TODO: handle exception
//            }
//        }
        try{
            TableUtils.createTable(connectionSource, DAOAccount.class);
            TableUtils.createTable(connectionSource, DAOPhotos.class);
            TableUtils.createTable(connectionSource, DAOCategory.class);
            TableUtils.createTable(connectionSource, DAOCardBank.class);
            TableUtils.createTable(connectionSource, DAOFood.class);
            TableUtils.createTable(connectionSource, DAOTable.class);

        } catch (Exception e ){
            GDB.log(getClass(),"... db fail to create tables." + e.getMessage());
        }

    }


    @Override
	public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

		try {
//            for(Class cls : tables){
//                TableUtils.dropTable(arg1, cls, true);
//
//            }
//			TableUtils.dropTable(arg1, DAOCategory.class, true);
//            TableUtils.dropTable(connectionSource, DAOAccount.class);
//            TableUtils.dropTable(connectionSource, DAOPhotos.class);
//            TableUtils.dropTable(connectionSource, DAOCategory.class);
//            TableUtils.dropTable(connectionSource, DAOCardBank.class);
//            TableUtils.dropTable(connectionSource, DAOFood.class,true);
//            TableUtils.dropTable(connectionSource, DAOTable.class,true);

//            TableUtils.createTable(connectionSource, DAOFood.class);
//            TableUtils.createTable(connectionSource, DAOTable.class);

        } catch (Exception e) {
//			// TODO: handle exception
		}
	}
	@Override
	public void close() {
		if (usageCounter.decrementAndGet() == 0) {
			super.close();
			helper = null;

		}
	}


    public Dao<DAOTable,Integer> getDAOTable() throws  SQLException{
        if(daoTable==null ) daoTable=getDao(DAOTable.class);
        return daoTable;
    }

    public Dao<DAOFood, Integer> getDAOFood() throws SQLException {
        if (daoFood == null) daoFood = getDao(DAOFood.class);
        return daoFood;
    }

    public Dao<DAOAccount, Integer> getDAOAccount() throws SQLException {
		if (daoAccount == null) daoAccount = getDao(DAOAccount.class);
		return daoAccount;
	}
	public Dao<DAOCategory,Integer> getDAOCategory() throws SQLException{
		if(daoCategory==null) daoCategory = getDao(DAOCategory.class);
		return daoCategory;
	}
	public Dao<DAOCardBank,Integer> getDAOCardBank() throws SQLException{
		if(daoCardBank==null)daoCardBank = getDao(DAOCardBank.class);
		return daoCardBank;
	}
	
	public Dao<DAOPhotos,Integer> getDAOPhotos() throws SQLException{
		if(daoPhoto==null)daoPhoto = getDao(DAOPhotos.class);
		return daoPhoto;
	}
}
